package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.databinding.ActivityRdCreateProjectBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.ClickUtil
import com.lee.common.utils.SharedPreUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.ProjectViewModel
import com.lee.common.network.getRequestBody
import com.lee.common.network.net.ObserverState
import com.xuexiang.xutil.common.StringUtils
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class RDCreateProjectActivity : BaseActivity<ActivityRdCreateProjectBinding>() {

    val viewModel by viewModels<ProjectViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_rd_create_project)
        setLeftVisible(true)
        setTitle("创建客户")

    }

    override fun initVew() {
        bindingView.superButtonCreate.setOnClickListener(View.OnClickListener {
            if (ClickUtil.isFastClick()) {
                return@OnClickListener
            }
            val etPtCreateUserS = bindingView.etRdCreateProject.text.toString().trim()
            if (StringUtils.isEmpty(etPtCreateUserS)) {
                ToastUtils.show(mContext, "请填写项目名称")
                return@OnClickListener
            }
            addAmibaProject(etPtCreateUserS)
        })
    }

    override fun setData() {}

    /**
     * app端添加研发项目接口
     * @param projectName
     */
    private fun addAmibaProject(projectName: String) {

        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        val map = mutableMapOf<String, Any>().apply {
            put("projectName", projectName)
            put("orgId", SharedPreUtils.getInteger("orgId", 0))
            put("createUser", SharedPreUtils.getString("userName"))
        }

        viewModel.addAmibaProject(getRequestBody(map))
            .observe(this, object : ObserverState<BaseBean>() {
                override fun onDataChange(data: BaseBean) {
                    customDialog.dismiss()
                    finishActivity()
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    ToastUtils.showShort(mContext, msg)
                    customDialog.dismiss()
                    super.onError(msg, code)
                }

            })
    }
}